<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="创建⼦联系⼈账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input :disabled="model.id" v-decorator="[ 'userName', validatorRules.userName]" placeholder="请输入数字/大小写字母"></a-input>
        </a-form-item>
        <a-form-item label="⼦联系⼈⼿机号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'phone', validatorRules.phone]" placeholder="请输入⼦联系⼈⼿机号码"></a-input>
        </a-form-item>
        <a-form-item label="登陆初始密码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          默认初始密码  lm@123456
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction,getAction } from '@/api/manage'
  import pick from 'lodash.pick'

  export default {
    name: "BlockManufacturerUserModal",
    components: {
    },
    props:{
      mainId:{
        type:String,
        required:false,
        default:''
      }
    },
    data () {
      return {
        listData:{},
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
        userName:{rules: [{ required: true, message: '请输入 创建人账号!' },{
              pattern: /^[0-9a-zA-Z]+$/,
              message: '账号格式错误！',
            }]},
        phone:{rules: [{ required: true, message: '请输入手机号码!' },{
              pattern: /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/,
              message: '手机号格式错误！',
            }]},
        },
        url: {
          add: "/block/blockManufacturerInfo/addBlockManufacturerUser",
          edit: "/block/blockManufacturerInfo/editBlockManufacturerUser",
        }

      }
    },
    mounted() {
      let list={
        column: "createTime",
        order: "desc",
        field: "id,,factoryName,companyProfile,createTime,type,name,status,address,phone,imageurl,action",
        pageNo: 1,
        pageSize: 5
      };
      getAction("/block/blockManufacturerInfo/list",list).then((res)=>{
        if (res.success) {
          this.listData = res.result.records[0];
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'createBy','createTime','updateBy','updateTime','userName','flag','phone','loginTime','manufacturerId'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            formData['manufacturerId'] = this.listData.id
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'createBy','createTime','updateBy','updateTime','userName','flag','phone','loginTime','manufacturerId'))
      },


    }
  }
</script>
